home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / FromTheMag / JW FLV MEDIA PLAYER 4.2 / mediaplayer.exe / player.swf / scripts / com / jeroenwijering / models / HTTPModel.as < prev    next >
Text File  |  2008-11-04  |  11KB  |  338 lines

  1. package com.jeroenwijering.models
  2. {
  3.    import com.jeroenwijering.events.ModelEvent;
  4.    import com.jeroenwijering.events.ModelStates;
  5.    import com.jeroenwijering.player.Model;
  6.    import com.jeroenwijering.utils.NetClient;
  7.    import flash.events.AsyncErrorEvent;
  8.    import flash.events.ErrorEvent;
  9.    import flash.events.IOErrorEvent;
  10.    import flash.events.NetStatusEvent;
  11.    import flash.events.SecurityErrorEvent;
  12.    import flash.media.SoundTransform;
  13.    import flash.media.Video;
  14.    import flash.net.NetConnection;
  15.    import flash.net.NetStream;
  16.    import flash.utils.clearInterval;
  17.    import flash.utils.setInterval;
  18.    
  19.    public class HTTPModel implements ModelInterface
  20.    {
  21.        
  22.       
  23.       private var stream:NetStream;
  24.       
  25.       private var keyframes:Object;
  26.       
  27.       private var h264:Boolean;
  28.       
  29.       private var loaded:Number;
  30.       
  31.       private var offset:Number;
  32.       
  33.       private var timeinterval:Number;
  34.       
  35.       private var connection:NetConnection;
  36.       
  37.       private var timeoffset:Number;
  38.       
  39.       private var model:Model;
  40.       
  41.       private var loadinterval:Number;
  42.       
  43.       private var transform:SoundTransform;
  44.       
  45.       private var video:Video;
  46.       
  47.       public function HTTPModel(param1:Model)
  48.       {
  49.          super();
  50.          model = param1;
  51.          connection = new NetConnection();
  52.          connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
  53.          connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  54.          connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR,errorHandler);
  55.          connection.connect(null);
  56.          stream = new NetStream(connection);
  57.          stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
  58.          stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  59.          stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler);
  60.          stream.bufferTime = model.config["bufferlength"];
  61.          stream.client = new NetClient(this);
  62.          video = new Video(320,240);
  63.          video.attachNetStream(stream);
  64.          transform = new SoundTransform();
  65.          stream.soundTransform = transform;
  66.          if(model.config["mute"] == true)
  67.          {
  68.             volume(0);
  69.          }
  70.          else
  71.          {
  72.             volume(model.config["volume"]);
  73.          }
  74.          quality(model.config["quality"]);
  75.          offset = timeoffset = 0;
  76.       }
  77.       
  78.       public function stop() : void
  79.       {
  80.          clearInterval(loadinterval);
  81.          clearInterval(timeinterval);
  82.          offset = timeoffset = 0;
  83.          h264 = false;
  84.          keyframes = undefined;
  85.          if(stream.bytesLoaded != stream.bytesTotal)
  86.          {
  87.             stream.close();
  88.          }
  89.          stream.pause();
  90.       }
  91.       
  92.       private function getToken() : String
  93.       {
  94.          return model.config["token"];
  95.       }
  96.       
  97.       private function loadHandler() : void
  98.       {
  99.          var _loc1_:* = undefined;
  100.          loaded = stream.bytesLoaded;
  101.          _loc1_ = stream.bytesTotal;
  102.          if(loaded >= _loc1_ && loaded > 0)
  103.          {
  104.             clearInterval(loadinterval);
  105.          }
  106.          model.sendEvent(ModelEvent.LOADED,{
  107.             "loaded":loaded,
  108.             "total":_loc1_ + offset,
  109.             "offset":offset
  110.          });
  111.       }
  112.       
  113.       public function quality(param1:Boolean) : void
  114.       {
  115.          if(param1 == true)
  116.          {
  117.             video.smoothing = true;
  118.             video.deblocking = 3;
  119.          }
  120.          else
  121.          {
  122.             video.smoothing = false;
  123.             video.deblocking = 1;
  124.          }
  125.       }
  126.       
  127.       private function getOffset(param1:Number, param2:Boolean = false) : Number
  128.       {
  129.          var _loc3_:* = undefined;
  130.          _loc3_ = 0;
  131.          while(_loc3_ < keyframes.times.length)
  132.          {
  133.             if(keyframes.times[_loc3_] <= param1 && (keyframes.times[_loc3_ + 1] >= param1 || !keyframes.times[_loc3_ + 1]))
  134.             {
  135.                if(param2 == true)
  136.                {
  137.                   return keyframes.times[_loc3_];
  138.                }
  139.                return keyframes.filepositions[_loc3_];
  140.             }
  141.             _loc3_++;
  142.          }
  143.          return 0;
  144.       }
  145.       
  146.       private function metaHandler(param1:ErrorEvent) : void
  147.       {
  148.          model.sendEvent(ModelEvent.META,{"error":param1.text});
  149.       }
  150.       
  151.       private function timeHandler() : void
  152.       {
  153.          var _loc1_:* = undefined;
  154.          var _loc2_:* = undefined;
  155.          var _loc3_:* = undefined;
  156.          _loc1_ = Math.round(stream.bufferLength / stream.bufferTime * 100);
  157.          _loc2_ = Math.round(stream.time * 10) / 10;
  158.          if(h264)
  159.          {
  160.             _loc2_ += timeoffset;
  161.          }
  162.          _loc3_ = model.playlist[model.config["item"]]["duration"];
  163.          if(_loc1_ < 100 && _loc2_ < Math.abs(_loc3_ - stream.bufferTime * 2))
  164.          {
  165.             model.sendEvent(ModelEvent.BUFFER,{"percentage":_loc1_});
  166.             if(model.config["state"] != ModelStates.BUFFERING && _loc1_ < 10)
  167.             {
  168.                model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
  169.             }
  170.          }
  171.          else if(model.config["state"] == ModelStates.BUFFERING)
  172.          {
  173.             model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  174.          }
  175.          if(_loc3_ > 0)
  176.          {
  177.             model.sendEvent(ModelEvent.TIME,{
  178.                "position":_loc2_,
  179.                "duration":_loc3_
  180.             });
  181.          }
  182.       }
  183.       
  184.       private function statusHandler(param1:NetStatusEvent) : void
  185.       {
  186.          if(param1.info.code == "NetStream.Play.Stop")
  187.          {
  188.             if(model.config["state"] != ModelStates.COMPLETED)
  189.             {
  190.                clearInterval(timeinterval);
  191.                model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
  192.             }
  193.          }
  194.          else if(param1.info.code == "NetStream.Play.StreamNotFound")
  195.          {
  196.             stop();
  197.             model.sendEvent(ModelEvent.ERROR,{"message":"Video stream not found: " + model.playlist[model.config["item"]]["file"]});
  198.          }
  199.          else
  200.          {
  201.             model.sendEvent(ModelEvent.META,{"info":param1.info.code});
  202.          }
  203.       }
  204.       
  205.       private function errorHandler(param1:ErrorEvent) : void
  206.       {
  207.          model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
  208.       }
  209.       
  210.       public function load() : void
  211.       {
  212.          var _loc1_:* = undefined;
  213.          model.mediaHandler(video);
  214.          if(stream.bytesLoaded != stream.bytesTotal)
  215.          {
  216.             stream.close();
  217.          }
  218.          _loc1_ = model.playlist[model.config["item"]]["file"];
  219.          if(model.config["streamer"] == "lighttpd")
  220.          {
  221.             if(h264)
  222.             {
  223.                _loc1_ += "?start=" + timeoffset;
  224.             }
  225.             else
  226.             {
  227.                _loc1_ += "?start=" + offset;
  228.             }
  229.          }
  230.          else if(model.config["streamer"].indexOf("?") > -1)
  231.          {
  232.             _loc1_ = model.config["streamer"] + "&file=" + _loc1_ + "&start=" + offset;
  233.          }
  234.          else
  235.          {
  236.             _loc1_ = model.config["streamer"] + "?file=" + _loc1_ + "&start=" + offset;
  237.          }
  238.          _loc1_ += "&id=" + model.config["id"];
  239.          _loc1_ += "&client=" + encodeURI(model.config["client"]);
  240.          _loc1_ += "&version=" + encodeURI(model.config["version"]);
  241.          _loc1_ += "&width=" + model.config["width"];
  242.          if(getToken())
  243.          {
  244.             _loc1_ += "&token=" + getToken();
  245.          }
  246.          stream.play(_loc1_);
  247.          clearInterval(loadinterval);
  248.          clearInterval(timeinterval);
  249.          loadinterval = setInterval(loadHandler,100);
  250.          timeinterval = setInterval(timeHandler,100);
  251.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
  252.       }
  253.       
  254.       public function onData(param1:Object) : void
  255.       {
  256.          var _loc2_:* = undefined;
  257.          if(param1.type == "metadata" && !h264)
  258.          {
  259.             if(param1.width)
  260.             {
  261.                video.width = param1.width;
  262.                video.height = param1.height;
  263.             }
  264.             if(param1.seekpoints)
  265.             {
  266.                h264 = true;
  267.                keyframes = new Object();
  268.                keyframes.times = new Array();
  269.                keyframes.filepositions = new Array();
  270.                for(_loc2_ in param1.seekpoints)
  271.                {
  272.                   keyframes.times[_loc2_] = Number(param1.seekpoints[_loc2_]["time"]);
  273.                   keyframes.filepositions[_loc2_] = Number(param1.seekpoints[_loc2_]["offset"]);
  274.                }
  275.             }
  276.             else if(param1.keyframes)
  277.             {
  278.                keyframes = param1.keyframes;
  279.             }
  280.             if(model.playlist[model.config["item"]]["start"] > 0)
  281.             {
  282.                seek(model.playlist[model.config["item"]]["start"]);
  283.             }
  284.             model.sendEvent(ModelEvent.META,param1);
  285.          }
  286.          else if(param1.type != "metadata")
  287.          {
  288.             model.sendEvent(ModelEvent.META,param1);
  289.          }
  290.       }
  291.       
  292.       public function volume(param1:Number) : void
  293.       {
  294.          transform.volume = param1 / 100;
  295.          stream.soundTransform = transform;
  296.       }
  297.       
  298.       public function play() : void
  299.       {
  300.          stream.resume();
  301.          timeinterval = setInterval(timeHandler,100);
  302.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  303.       }
  304.       
  305.       public function seek(param1:Number) : void
  306.       {
  307.          var _loc2_:* = undefined;
  308.          clearInterval(timeinterval);
  309.          _loc2_ = getOffset(param1);
  310.          if(_loc2_ < offset || _loc2_ > offset + loaded)
  311.          {
  312.             offset = _loc2_;
  313.             timeoffset = getOffset(param1,true);
  314.             load();
  315.          }
  316.          else
  317.          {
  318.             if(h264)
  319.             {
  320.                stream.seek(param1 - timeoffset);
  321.             }
  322.             else
  323.             {
  324.                stream.seek(param1);
  325.             }
  326.             play();
  327.          }
  328.       }
  329.       
  330.       public function pause() : void
  331.       {
  332.          clearInterval(timeinterval);
  333.          stream.pause();
  334.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
  335.       }
  336.    }
  337. }
  338.